package models

import play.api.data._
import play.api.data.Forms._
import play.api.db.DB;
import play.api.db._
import play.api.Play.current
import anorm._
import anorm.SqlParser._

case class Message(id:Long,label:String)

object Message{

    val message={

        get[Long]("id")~
        get[String]("label") map{
        
            case id~label=>Message(id,label)

        }

    }
	
	def all():List[Message]=DB.withConnection{implicit c=>

        SQL("select * from message").as(message*)

    }

	def create(label:String){
        DB.withConnection{implicit c=>
            SQL("insert into message(label) values ({label})").on(
                'label->label
            ).executeUpdate()
        }
	}

	def delete(id:Long){
	    DB.withConnection{implicit c=>
            SQL("delete from message where id={id}").on (
                'id->id
            ).executeUpdate()
        }	
	}

	def update(id:Long,label:String){
		
		val sql = "UPDATE message SET label = "+label+" WHERE id ="+id;
		DB.withConnection{implicit c=>
			SQL(sql).executeUpdate()
		}
	}
	
	val messageForm=Form(

	    "label"->nonEmptyText

	)
}
